package com.wxq.workpad.utils;

import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

import com.wxq.workpad.base.RealShow;

import java.util.ArrayList;
import java.util.List;


public class FragmentChangeManager {
    private FragmentManager mFragmentManager;
    private int mContainerViewId;
    /**
     * Fragment切换数组
     */
    private List<Fragment> mFragments;
    /**
     * 当前选中的Tab
     */
    private int mCurrentTab;

    public FragmentChangeManager(FragmentManager fm, int containerViewId, List<Fragment> fragments) {
        this.mFragmentManager = fm;
        this.mContainerViewId = containerViewId;
        this.mFragments = fragments;
        initFragments();
    }

    /**
     * 初始化fragments
     */
    private void initFragments() {
        for (Fragment fragment : mFragments) {
            mFragmentManager.beginTransaction()
                    .add(mContainerViewId, fragment,fragment.getClass().getName() + mFragments.indexOf(fragment)).commitAllowingStateLoss();
        }


    showCurrentFragment(0);

//            setFragments(0);

    }

    /**
     * 界面切换控制
     */
    public void setFragments(int index) {

        mFragmentManager.beginTransaction()
                .add(mContainerViewId, mFragments.get(index),mFragments.get(index).getClass().getName() + index).commitAllowingStateLoss();
        showCurrentFragment(index);
    }

    public void showCurrentFragment(int index) {
        for (int i = 0; i < mFragments.size(); i++) {
            FragmentTransaction ft = mFragmentManager.beginTransaction();
            Fragment fragment = mFragments.get(i);
            if (i == index) {
                ft.show(fragment);
                if (fragment instanceof RealShow) {
                    ((RealShow) fragment).realShow();
                }
            } else {
                ft.hide(fragment);
            }
            ft.commitAllowingStateLoss();
        }
        mCurrentTab = index;
    }

//    public void setCurrentTab(int currentTab) {
//
//
//                int tempCurrentTab = getFragmentIndex(mCurrentTab);
//                Fragment ft=mFragmentChangeManager.getCurrentFragment(tempCurrentTab);
//                if (!ft.isAdded()&&null == mFragmentChangeManager.getFragmentByTag(ft.getClass().getName())){
//                    mFragmentChangeManager.setFragments(tempCurrentTab);
//                }else {
//                    mFragmentChangeManager.showCurrentFragment(tempCurrentTab);
//                }
////                if (mFragmentChangeManager.getCurrentFragment(tempCurrentTab).isAdded()) {
////                    mFragmentChangeManager.showCurrentFragment(tempCurrentTab);
////                } else {
////                    mFragmentChangeManager.setFragments(tempCurrentTab);
////                }
//
//
//
//    }



    public int getCurrentTab() {
        return mCurrentTab;
    }

    public Fragment getCurrentFragment(int index) {
        return mFragments.get(index);
    }

    public Fragment getFragmentByTag(String tag){
        return mFragmentManager.findFragmentByTag(tag);
    }
}